/*
 * Copyright (c) 2015. For Intelligent Group.
 */

package com.intelligent.ispc.core.service.impl;

import com.google.common.collect.Lists;
import com.intelligent.ispc.cache.TestDataCacheFactory;
import com.intelligent.ispc.common.mapper.JsonMapper;
import com.intelligent.ispc.core.entity.TestData;
import com.intelligent.ispc.core.service.BaseCacheService;
import com.intelligent.ispc.utils.YieldType;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.regex.Pattern;

/**
 * Created by cherry on 2016/3/15.
 */

public class BaseCacheServiceImpl implements BaseCacheService {

    @Autowired
    protected TestDataCacheFactory testDataCacheFactory;

    protected JsonMapper mapper = JsonMapper.nonDefaultMapper();

    @Override
    public List<TestData> searchTestDataList(Map<String, Object> searchParams, Pageable pageable) {

        List<TestData> testDataList = Lists.newArrayList();
        try {
            if (pageable != null ) {
                searchParams.put("pageNumber", pageable.getPageNumber());
                searchParams.put("pageSize", pageable.getPageSize());
            }

            String strList = testDataCacheFactory.getCacheTestDatas().get(mapper.toJson(getParameters(searchParams)));
            testDataList = mapper.fromJson(strList, mapper.contructCollectionType(List.class, TestData.class));

            return testDataList;
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return testDataList;
    }

    @Override
    public Long getTestDateCount(Map<String, Object> searchParams) {

        Long count = 0L;
        try {
            if (searchParams.containsKey("pageNumber")) {
                searchParams.remove("pageNumber");
            }

            if (searchParams.containsKey("pageSize")) {
                searchParams.remove("pageSize");
            }
            String strCount = testDataCacheFactory.getCacheTestDataCount().get(mapper.toJson(getParameters(searchParams)));
            List<Long> countList = mapper.fromJson(strCount, mapper.contructCollectionType(List.class, Long.class));
            count = countList.get(0);

        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public List<String> getExcludeIds(Map<String, Object> searchParams) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (searchParams.containsKey("projectId")) {
            map.put("projectId", searchParams.get("projectId"));
        }

        if (searchParams.containsKey("userId")) {
            map.put("userId", searchParams.get("userId"));
        }
        List<String> results = new ArrayList<String>();
        try {
            String excludeIds = testDataCacheFactory.getCacheExcludeIds().get(mapper.toJson(map));
            results = mapper.fromJson(excludeIds, mapper.contructCollectionType(List.class, String.class));
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        return results;
    }

    private Map<String, Object> getParameters(Map<String, Object> searchParams) {

        Map<String, Object> map = new HashMap<String, Object>();

        if (searchParams.containsKey("pageNumber")) {
            map.put("pageNumber", searchParams.get("pageNumber"));
        }

        if (searchParams.containsKey("pageSize")) {
            map.put("pageSize", searchParams.get("pageSize"));
        }

        if (searchParams.containsKey("ids")) {
            map.put("ids", searchParams.get("ids"));
        }

        if (searchParams.containsKey("id")) {
            map.put("id", searchParams.get("id"));
        }

        if (searchParams.containsKey("projectId")) {
            map.put("projectId", searchParams.get("projectId"));
        }

        if (searchParams.containsKey("itemName")) {
            map.put("itemName", searchParams.get("itemName"));
        }

        if (searchParams.containsKey("stationId")) {
            map.put("stationId", searchParams.get("stationId"));
        }

        if (searchParams.containsKey("slotNo")) {
            map.put("slotNo", searchParams.get("slotNo"));
        }


        if (searchParams.containsKey("serialNumber")) {
            map.put("serialNumber", searchParams.get("serialNumber"));
        }


        if (searchParams.containsKey("testFileUUID")) {
            map.put("testFileUUID", searchParams.get("testFileUUID"));
        }

        if (searchParams.containsKey("type")) {
            map.put("type", searchParams.get("type"));
        }

        if (searchParams.containsKey("loopCount")) {
            map.put("loopCount", searchParams.get("loopCount"));
        }

        if (searchParams.containsKey("isPass")) {
            map.put("isPass", searchParams.get("isPass"));
        }

        if (searchParams.containsKey("isDelete")) {
            map.put("isDelete", searchParams.get("isDelete"));
        }

        if (searchParams.containsKey("startTime")) {
            map.put("startTime", searchParams.get("startTime"));
        }

        if (searchParams.containsKey("stopTime")) {
            map.put("stopTime", searchParams.get("stopTime"));
        }

        if (searchParams.containsKey("DESC")) {
            map.put("DESC", searchParams.get("DESC"));
        }

        if (searchParams.containsKey("isNeedItems")) {
            map.put("isNeedItems", searchParams.get("isNeedItems"));
        }

        if (searchParams.containsKey("isNeedDutAttributes")) {
            map.put("isNeedDutAttributes", searchParams.get("isNeedDutAttributes"));
        }

        if (searchParams.containsKey("stationIdOrSlotNo")) {
            map.put("stationIdOrSlotNo", searchParams.get("stationIdOrSlotNo"));
        }


        return map;
    }

}
